﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace objass
{
    /// <summary>
    /// Interaction logic for Edit2Window.xaml
    /// </summary>
    public partial class EditRelWindow : UserControl
    {
        public EditRelWindow()
        {
            InitializeComponent();

            // 示例数据
            var relations = new List<Relation>
                {
                };

            RelGrid.ItemsSource = relations;
        }

        private void UpdateRelGrid(string nodeA)
        {
            var app = (App)Application.Current;
            var relNodes = app.GetRelNodes(nodeA);
            List<Relation> datas = new List<Relation>();
            foreach (var relNode in relNodes)
            {
                var data = new Relation();
                data.Rel = relNode.Value;
                data.NodeB = relNode.Key;
                datas.Add(data);
            }
            RelGrid.ItemsSource = datas;
            labelRel.Content = $"已保存关联数: {datas.Count}";
        }

        private void BtnSelectNodeA_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new NodeInput();
            if (dialog.ShowDialog() == true)
            {
                LabelNodeA.Content= dialog.SelectedWord;
                UpdateRelGrid(dialog.SelectedWord);
            }
        }

        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new EditRel();
            dialog.NodeNameA = LabelNodeA.Content.ToString();
            //RelGrid当前选中的行的节点B字段，赋值给dialog.NodeNameB
            var selectedItem = RelGrid.SelectedItem as Relation;
            if (selectedItem != null)
            {
                dialog.NodeNameB.Add(selectedItem.NodeB);
            }
            if (dialog.ShowDialog() == true)
            {
                UpdateRelGrid(dialog.NodeNameA);
            }
        }

        private void BtnDel_Click(object sender, RoutedEventArgs e)
        {
            // 处理删除关联的逻辑
            var selectedItem = RelGrid.SelectedItem as Relation;
            if (selectedItem != null)
            {
                var app = (App)Application.Current;
                app.DeleteRel(LabelNodeA.Content.ToString(), selectedItem.NodeB);
                UpdateRelGrid(LabelNodeA.Content.ToString());
            }
        }

        private void BtnCopyInfo(object sender, RoutedEventArgs e)
        {
            //复制信息的格式:
            //   节点A: [节点A名称]
            //   [关联名称] -> [节点B名称]
            //   [关联名称] -> [节点B名称]
            //   节点B列表: [节点B名称1], [节点B名称2], ...
            string text;
            text = $"节点A: {LabelNodeA.Content.ToString()}\n";
            var app = (App)Application.Current;
            var relNodes = app.GetRelNodes(LabelNodeA.Content.ToString());
            foreach (var relNode in relNodes)
            {
                text += $"{relNode.Value} -> {relNode.Key}\n";
            }
            text += $"节点B列表: {string.Join(", ", relNodes.Keys)}";
            Clipboard.SetDataObject(text);
            //Clipboard.Clear();
            //Clipboard.SetText(text);
            // 弹出一个 复制成功的tip
            MessageBox.Show("复制成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
    public class Relation
    {
        public string Rel { get; set; }
        public string NodeB { get; set; }
    }
}
